package com.deltabravomedia.theoldtoad;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {	
	private final String TAG = "Map";
	
	private GeoPoint oldToad = new GeoPoint(43153263,-77595823);
	private GeoPoint currentLocation = null;
	
	private static final int MIN_LOCATION_UPDATE_DISTANCE = 30;
	private static final int MIN_LOCATION_UPDATE_TIME = 60000;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.map);
	    
        // Capture our button from layout
        Button directionsButton = (Button)findViewById(R.id.directions_button);
        directionsButton.setBackgroundColor(Color.YELLOW);
        // Register the onClick listener with the implementation above
        directionsButton.setOnClickListener(mDirectionsListener);
	    
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.mapicon);
	    OldToadMapOverlay itemizedoverlay = new OldToadMapOverlay(drawable, this);
	    OverlayItem overlayitem = new OverlayItem(oldToad, "The Old Toad", "277 Alexander Street");
	    itemizedoverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedoverlay);

		LocationManager locator = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	 // Start with fine location.
	    Location l = locator.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	    if (l == null) {
		  Log.w(TAG, "No previous fine location found!");
	      // Fall back to coarse location.
	      l = locator.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	      
	      if (l == null)
	      {
	    	  	Log.w(TAG, "No previous coarse location found!");
	    		LocationListener mLocationListener = new LocationListener() {
	    			  public void onLocationChanged(Location location) {
	    			    if (location != null) {
	    			      location.getAltitude();
	    			      location.getLatitude();
	    			      location.getLongitude();
	    			      location.getTime();
	    			      location.getAccuracy();
	    			      location.getSpeed();
	    			      location.getProvider();
	    			    }
	    			  }

	    			  public void onProviderDisabled(String provider) {
	    			    // ...
	    			  }

	    			  public void onProviderEnabled(String provider) {
	    			    // ...
	    			  }

	    			  public void onStatusChanged(String provider, int status, Bundle extras) {
	    			    // ...
	    			  }
	    			};
	    			
	    			// You need to specify a Criteria for picking the location data source.
	    			// The criteria can include power requirements.
	    			Criteria criteria = new Criteria();
	    			criteria.setAccuracy(Criteria.ACCURACY_COARSE);  // Faster, no GPS fix.
	    			//criteria.setAccuracy(Criteria.ACCURACY_FINE);  // More accurate, GPS fix.
	    			// You can specify the time and distance between location updates.
	    			// Both are useful for reducing power requirements.
	    			locator.requestLocationUpdates(locator.getBestProvider(criteria, true),
	    			    MIN_LOCATION_UPDATE_TIME, MIN_LOCATION_UPDATE_DISTANCE, mLocationListener,
	    			    getMainLooper());
	    			
	    			l = locator.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);	    	  
	      }
	    }

	    if (l != null)
	    {
			Drawable drawable2 = this.getResources().getDrawable(R.drawable.youmapicon);
			OldToadMapOverlay itemizedoverlay2 = new OldToadMapOverlay(drawable2, this);
			int lat = (int)(l.getLatitude() * 1000000);
			int lng = (int)(l.getLongitude() * 1000000);
			currentLocation = new GeoPoint(lat, lng);
			OverlayItem overlayitem2 = new OverlayItem(currentLocation, "You", "You are here!");
			itemizedoverlay2.addOverlay(overlayitem2);
			mapOverlays.add(itemizedoverlay2);
	    } else {
	    	currentLocation = new GeoPoint(0,0);
	    	Log.w(TAG, "No location found!");
	    	//directionsButton.setVisibility(View.GONE);
	    }
	    		
	    MapController mapController = mapView.getController();
	    mapController.setCenter(oldToad);
	    mapController.setZoom(14);
	}
	
	 // Create an anonymous implementation of OnClickListener
    private OnClickListener mDirectionsListener = new OnClickListener() {
        public void onClick(View v) {
        	int toLat = oldToad.getLatitudeE6();
        	int toLng = oldToad.getLongitudeE6();
        	int fromLat = currentLocation.getLatitudeE6();
        	int fromLng = currentLocation.getLongitudeE6();
        	
        	Log.w(TAG, "Build.VERSION.RELEASE: " + Build.VERSION.RELEASE);
        	String release = Build.VERSION.RELEASE;
        	int majorRelease = Integer.parseInt(release.substring(0, 1));
        	
        	if (majorRelease < 1)
        	{
        		Intent i = new Intent(Map.this, Directions.class);
	            i.putExtra("toLat", toLat);
	            i.putExtra("toLng", toLng);
	            i.putExtra("fromLat", fromLat);
	            i.putExtra("fromLng", fromLng);
	        	startActivity(i);        		
        	} else {
        		String location = "177+Alexander+St,+Rochester+NY+14607";
        		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + location)); 
        		startActivity(i);
        	}
        }
    };

	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
}
